package com.eastcom.bm.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.eastcom.bm.atom.Atom_DatasetDefine;
import com.eastcom.bm.atom.Atom_KpiDefine;
import com.eastcom.bm.dao.AbstractDAO;
import com.eastcom.bm.dao.IKpiDefineDao;
import com.eastcom.common.db.JDBCTemplate;
import com.eastcom.common.db.ResultData;


public class KpiDefineDao extends AbstractDAO implements IKpiDefineDao {

	public List<Atom_KpiDefine> getAllKpiDefine() throws Exception {
		try{
			List<Atom_KpiDefine> results=new ArrayList<Atom_KpiDefine>();

			String sql="select id,kpi_id,name_cn,dataset_id,validation_type,baseline_value,deviation_up," +
					"deviation_down,threadhold_up,threadhold_down,calendar_policy_uuid from qa_kpi_define";
			
			Atom_DatasetDefine datasetDefine=null;
			Map<String,Atom_DatasetDefine> dataSetMap=new HashMap<String,Atom_DatasetDefine>();
			ResultData rd=JDBCTemplate.executeSQLQuery(sql, JDBCTemplate.PROPAGATION_NEVER, false, false);
			while(rd.next()){
				Atom_KpiDefine kpiDefine=new Atom_KpiDefine();
				
				kpiDefine.setId(rd.getString("ID"));
				kpiDefine.setKpiId(rd.getString("KPI_ID"));
				kpiDefine.setCn_name(rd.getString("NAME_CN"));
				
				datasetDefine=dataSetMap.get(rd.getString("DATASET_ID"));
				if(datasetDefine==null){
					datasetDefine=new Atom_DatasetDefine();
					datasetDefine.setId(rd.getString("DATASET_ID"));
				}
				
				kpiDefine.setDatasetDefine(datasetDefine);
				kpiDefine.setValidationType(rd.getString("VALIDATION_TYPE"));
				kpiDefine.setBaselineValue(rd.getDouble("BASELINE_VALUE"));
				kpiDefine.setDeviationUp(rd.getDouble("DEVIATION_UP"));
				kpiDefine.setDeviationDown(rd.getDouble("DEVIATION_DOWN"));
				kpiDefine.setThreadHoldUp(rd.getDouble("THREADHOLD_UP"));
				kpiDefine.setThreadHoldDown(rd.getDouble("THREADHOLD_DOWN"));
				kpiDefine.setCalendarPolicyUUID(rd.getString("CALENDAR_POLICY_UUID"));
				results.add(kpiDefine);
			}

			return results;
		}
		catch(Exception e){
			throw e;
		}
	}
	
	public List<Atom_KpiDefine> selectKpiDefineByDataSetId(String dataSetId) throws Exception{
		try{
			List<Atom_KpiDefine> results=new ArrayList<Atom_KpiDefine>();

			String sql="select id,kpi_id,name_cn,dataset_id,validation_type,baseline_value,deviation_up," +
					"deviation_down,threadhold_up,threadhold_down,calendar_policy_uuid from qa_kpi_define " +
					"where dataset_id='"+dataSetId+"'";
			
			Atom_DatasetDefine datasetDefine=null;
			Map<String,Atom_DatasetDefine> dataSetMap=new HashMap<String,Atom_DatasetDefine>();
			ResultData rd=JDBCTemplate.executeSQLQuery(sql, JDBCTemplate.PROPAGATION_NEVER, false, false);
			while(rd.next()){
				Atom_KpiDefine kpiDefine=new Atom_KpiDefine();
				
				kpiDefine.setId(rd.getString("ID"));
				kpiDefine.setKpiId(rd.getString("KPI_ID"));
				kpiDefine.setCn_name(rd.getString("NAME_CN"));
				
				datasetDefine=dataSetMap.get(rd.getString("DATASET_ID"));
				if(datasetDefine==null){
					datasetDefine=new Atom_DatasetDefine();
					datasetDefine.setId(rd.getString("DATASET_ID"));
				}
				
				kpiDefine.setDatasetDefine(datasetDefine);
				kpiDefine.setValidationType(rd.getString("VALIDATION_TYPE"));
				kpiDefine.setBaselineValue(rd.getDouble("BASELINE_VALUE"));
				kpiDefine.setDeviationUp(rd.getDouble("DEVIATION_UP"));
				kpiDefine.setDeviationDown(rd.getDouble("DEVIATION_DOWN"));
				kpiDefine.setThreadHoldUp(rd.getDouble("THREADHOLD_UP"));
				kpiDefine.setThreadHoldDown(rd.getDouble("THREADHOLD_DOWN"));
				kpiDefine.setCalendarPolicyUUID(rd.getString("CALENDAR_POLICY_UUID"));
				results.add(kpiDefine);
			}

			return results;
		}
		catch(Exception e){
			throw e;
		}
	}
	
}
